﻿Imports Iesi.Collections.Generic
Imports KanBan.Pgd.Domain.Exceptions
Imports KanBan.Pgd.Domain.ValueObjects

Namespace Entities

    ''' <summary>
    ''' Representa una Solicitud de Búsqueda de personal
    ''' </summary>
    ''' <remarks></remarks>
    Public Class SearchRequest
        Implements IDomainEntity

        Protected Sub New()

        End Sub

        Public Sub New(ByVal id As Guid, ByVal requester As User, ByVal searchedProfile As Profile, _
                       ByVal searchedProfileLevel As ProfileLevel, ByVal priority As Priority)

            Dim cachedDateTime As DateTime = DateTime.Now

            Me.Id = id
            Me.Sku = sku
            Me.Requester = requester
            Me.SearchedProfile = searchedProfile
            Me.SearchedProfileLevel = searchedProfileLevel
            Me.Priority = priority
            Me.CreatedDate = cachedDateTime
            Me.LastModified = cachedDateTime

        End Sub

        Public Sub New(ByVal requester As User, ByVal searchedProfile As Profile, _
                       ByVal searchedProfileLevel As ProfileLevel, ByVal priority As Priority)

            Me.New(Guid.NewGuid(), requester, searchedProfile, searchedProfileLevel, priority)

        End Sub

        ''' <summary>
        ''' Id de la Solicitud
        ''' </summary>
        ''' <remarks></remarks>
        Private _id As Guid
        Public Overridable Property Id() As Guid Implements IDomainEntity.Id
            Get
                Return _id
            End Get
            Set(ByVal value As Guid)
                _id = value
            End Set
        End Property

        Private _sku As String
        Public Overridable Property Sku() As String
            Get
                Return _sku
            End Get
            Set(ByVal value As String)
                _sku = value
            End Set
        End Property

        ''' <summary>
        ''' Solicitante de la Búsqueda
        ''' </summary>
        ''' <remarks></remarks>
        Private _requester As User
        Public Overridable Property Requester() As User
            Get
                Return _requester
            End Get
            Set(ByVal value As User)
                _requester = value
            End Set
        End Property

        ''' <summary>
        ''' Persona que responsable de llevar a cabo la Búsqueda
        ''' </summary>
        ''' <remarks></remarks>
        Private _responsible As User
        Public Overridable Property Responsible() As User
            Get
                Return _responsible
            End Get
            Set(ByVal value As User)
                _responsible = value
            End Set
        End Property

        ''' <summary>
        ''' Perfil buscado
        ''' </summary>
        ''' <remarks></remarks>
        Private _searchedProfile As Profile
        Public Overridable Property SearchedProfile() As Profile
            Get
                Return _searchedProfile
            End Get
            Set(ByVal value As Profile)
                _searchedProfile = value
            End Set
        End Property

        ''' <summary>
        ''' Nivel del Perfil buscado
        ''' </summary>
        ''' <remarks></remarks>
        Private _searchedProfileLevel As ProfileLevel
        Public Overridable Property SearchedProfileLevel() As ProfileLevel
            Get
                Return _searchedProfileLevel
            End Get
            Set(ByVal value As ProfileLevel)
                _searchedProfileLevel = value
            End Set
        End Property

        ''' <summary>
        ''' Tipo de contratación deseada
        ''' </summary>
        ''' <remarks></remarks>
        Private _recruitmentType As RecruitmentType
        Public Overridable Property RecruitmentType() As RecruitmentType
            Get
                Return _recruitmentType
            End Get
            Set(ByVal value As RecruitmentType)
                _recruitmentType = value
            End Set
        End Property

        ''' <summary>
        ''' Prioridad de la Búsqueda
        ''' </summary>
        ''' <remarks></remarks>
        Private _priority As Priority
        Public Overridable Property Priority() As Priority
            Get
                Return _priority
            End Get
            Set(ByVal value As Priority)
                _priority = value
            End Set
        End Property

        ''' <summary>
        ''' Area a la que pertenece la Solicitud de Búsqueda
        ''' </summary>
        ''' <remarks></remarks>
        Private _area As Area
        Public Overridable Property Area() As Area
            Get
                Return _area
            End Get
            Set(ByVal value As Area)
                _area = value
            End Set
        End Property

        ''' <summary>
        ''' Indica si la Solicitud de Búsqueda esta publicada o no
        ''' </summary>
        ''' <remarks></remarks>
        Private _isPublished As Boolean = False
        Public Overridable Property IsPublished() As Boolean
            Get
                Return _isPublished
            End Get
            Set(ByVal value As Boolean)
                _isPublished = value
            End Set
        End Property

        ''' <summary>
        ''' Estado de la Búsqueda
        ''' </summary>
        ''' <remarks></remarks>
        Private _status As SearchRequestStatus = SearchRequestStatus.Created
        Public Overridable Property Status() As SearchRequestStatus
            Get
                Return _status
            End Get
            Set(ByVal value As SearchRequestStatus)
                _status = value
            End Set
        End Property

        ''' <summary>
        ''' Descripción
        ''' </summary>
        ''' <remarks></remarks>
        Private _description As String
        Public Overridable Property Description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property

        ''' <summary>
        ''' Comentarios
        ''' </summary>
        ''' <remarks></remarks>
        Private _comments As String
        Public Overridable Property Comments() As String
            Get
                Return _comments
            End Get
            Set(ByVal value As String)
                _comments = value
            End Set
        End Property

        Private _candidates As ISet(Of Candidate) = New HashedSet(Of Candidate)
        Public Overridable Property Candidates() As ISet(Of Candidate)
            Get
                Return _candidates
            End Get
            Set(ByVal value As ISet(Of Candidate))
                _candidates = value
            End Set
        End Property

        Private _interviews As ISet(Of Interview) = New HashedSet(Of Interview)
        Public Overridable Property Interviews() As ISet(Of Interview)
            Get
                Return _interviews
            End Get
            Set(ByVal value As ISet(Of Interview))
                _interviews = value
            End Set
        End Property

        Public Overridable Function GetInterviewById(ByVal interviewId As Guid) As Interview

            Return (From i In Me.Interviews _
                    Where i.Id = interviewId _
                    Select i).SingleOrDefault()

        End Function

        Private _createdDate As DateTime

        Public Overridable Property CreatedDate() As Date Implements IDomainEntity.CreatedDate
            Get
                Return _createdDate
            End Get
            Set(ByVal value As Date)
                _createdDate = value
            End Set
        End Property

        Private _lastModified As DateTime
        Public Overridable Property LastModified() As Date Implements IDomainEntity.LastModified
            Get
                Return _lastModified
            End Get
            Set(ByVal value As Date)
                _lastModified = value
            End Set
        End Property

        ''' <summary>
        ''' Indica si la solicitud ha sido borrada
        ''' </summary>
        ''' <remarks></remarks>
        Private _isDeleted As Boolean = False
        Public Overridable Property IsDeleted() As Boolean Implements IDomainEntity.IsDeleted
            Get
                Return _isDeleted
            End Get
            Set(ByVal value As Boolean)
                _isDeleted = value
            End Set
        End Property

        Public Overridable Sub Validate() Implements IValidableEntity.Validate

            Throw New NotImplementedException()

        End Sub

        Public Overridable Sub ValidateDelete() Implements IValidableEntity.ValidateDelete

            Dim errors As New List(Of EntityValidationError)()

            If Me.Status > SearchRequestStatus.Created Then
                errors.Add(New EntityValidationError("Status", "You can't delete a Search Request wich status is greater than 'Created'"))
            End If

            If errors.Count > 0 Then
                Throw New EntityValidationException(errors)
            End If

        End Sub

        Public Overridable Sub SetStatus(ByVal status As SearchRequestStatus)

            Select Case status
                Case SearchRequestStatus.Created
                    If Me.Status > SearchRequestStatus.Created Then
                        Throw New BusinessRuleException("You can't set a Search Request back to 'Created' status")
                    End If
                Case SearchRequestStatus.InProgress
                    If Me.Status > SearchRequestStatus.InProgress Then
                        Throw New BusinessRuleException("You can't set a Search Request back to 'In Progress' status")
                    Else
                        Me.Status = status
                    End If
                Case SearchRequestStatus.Finished
                    If Me.Status <> SearchRequestStatus.InProgress Then
                        Throw New BusinessRuleException("You can't set a Search Request that is not 'In Progress' to 'Finished'")
                    Else
                        Me.Status = status
                    End If
                Case Else
                    Throw New BusinessRuleException("Invalid status")
            End Select

        End Sub

        Public Overridable Sub RemoveCandidates(ByVal candidateIds As List(Of Guid))

            For Each id As Guid In candidateIds
                RemoveCandidate(id)
            Next

        End Sub

        Public Overridable Sub RemoveCandidate(ByVal candidateId As Guid)

            For Each c As Candidate In Me.Candidates
                If c.Id = candidateId Then
                    Me.Candidates.Remove(c)
                    Exit For
                End If
            Next

        End Sub

        Public Overridable Sub AddCandidates(ByVal candidates As List(Of Candidate))

            For Each c As Candidate In candidates
                Me.Candidates.Add(c)
            Next

        End Sub

        Public Overridable Function HasCandidate(ByVal candidate As Candidate) As Boolean

            Return Me.Candidates.Contains(candidate)

        End Function

        'Public Overridable Sub RemoveInterview(ByVal interviewId As Guid)

        '    For Each i As Interview In Me.Interviews
        '        If i.Id = interviewId Then
        '            Me.Interviews.Remove(i)
        '            Exit For
        '        End If
        '    Next

        'End Sub

        Public Overridable Function GetAllInterviews() As IEnumerable(Of Interview)

            Dim nonDeletedInterviews As New List(Of Interview)

            For Each i As Interview In Me.Interviews
                If Not i.IsDeleted Then
                    nonDeletedInterviews.Add(i)
                End If
            Next

            Return nonDeletedInterviews

        End Function

        Public Overridable Function CandidateHasInterview(ByVal candidateId As Guid) As Boolean

            For Each i As Interview In Me.GetAllInterviews()
                If i.Candidate.Id = candidateId Then
                    Return True
                End If
            Next

            Return False

        End Function

        Public Overridable Sub RemoveInterviewsForCandidate(ByVal candidateId As Guid)

            For Each i As Interview In Me.GetAllInterviews()
                If i.Candidate.Id = candidateId Then
                    i.IsDeleted = True
                End If
            Next

        End Sub

        Public Overridable Sub RemoveInterview(ByVal interviewId As Guid)

            Dim interview = Me.Interviews.SingleOrDefault(Function(i) i.Id = interviewId)

            If interview IsNot Nothing Then
                Me.Interviews.Remove(interview)
            End If

        End Sub

    End Class

End Namespace